var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	initPages();
	window.onresize = function () {
		initPages();
	};
	pageBtns();
	getUserInfo();
});
function getUserInfo(){
	var seachUrl = decodeURI(window.location.search);
	var paramStr = seachUrl.substring(1);
	params = paramStr.split("&");
	var parm = {};
	for(var i = 0; i < params.length; i++){
		var key = params[i].split("=")[0];
		var value = params[i].split("=")[1];
		parm[key] = value;
	}
	for(var key in parm){
		if(parm[key] == "" || parm[key] == undefined || parm[key] == null){
			window.location.href = encodeURI("login.html");
			return false;
			break;
		}
	}
	$("#user-name").text(substr(parm.username,4)).attr("title",parm.username).attr("key",parm.userkey).attr("code",parm.usercode).attr("usertype",2).attr("schoolcode",parm.schoolcode);
	$("#user-head").attr("src","../"+ ServiceName + parm.headimg);
	checkUser();
}
function checkUser(){
	var userKey = $("#user-name").attr("key");
	var cellphone = $("#user-name").attr("code");
	var data = {
			cmd:"getTeacher",
			model:"teacher",
			userKey:userKey,
			data:{
				cellphone:cellphone
			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(cres){
		if(cres.result == 1 && cres.data != undefined){
			var teachercode = cres.data.teachercode;
			var classcode = cres.data.classcode;
			$("#user-name",parent.document).attr("teachercode",teachercode).attr("classcode",classcode);
			if(classcode == null || classcode == "null"){
				$("#tindex-position").hide();
			}else{
				$("#tindex-position").show();
			}	
			changePage(PageUrl.tschoolManageUrl);	
			
		}
	},null,null);
}

function substr(str,len){
	if(str != undefined){
		if(str.length > len){
			str = str.substring(0,len-1)+"...";
			return str;
		}else{
			return str;
		}
	}else{
		return "";
	}
}
function initPages(){
	var winH = document.documentElement.clientHeight || document.body.clientHeight;
	var bodyH = winH - $(".index-head").height() - $(".footer").height()-5;
	$(".index-body").height(bodyH);
}

function pageBtns(){
	$(".head-menu").click(function(){
		$(".head-menu").removeClass("head-menu-select");
		if($(this).text() == "新闻公告"){
			$(this).addClass("head-menu-select");
			changePage(PageUrl.tnewsManageUrl);		
		}else if($(this).text() == "校务管理"){
			$(this).addClass("head-menu-select");
			changePage(PageUrl.tschoolManageUrl);				
		}else if($(this).text() == "位置服务"){
			$(this).addClass("head-menu-select");
			changePage(PageUrl.tpositionManageUrl);				
		}
	});
	
	$(".logout-btns").click(function(){
		window.location.href = encodeURI("login.html");
	});
	
	$(".person-set-btns").click(function(){
		var usercode = $("#user-name").attr("code");
		var userKey = $("#user-name").attr("key");
		var usertype = $("#user-name").attr("usertype");
		var url = "setting.html?usercode="+usercode+"&userkey="+userKey+"&usertype="+usertype;
		window.open(url);
	});
	
	$(".user-info").mouseover(function(){
		$("#navigate").show();
	});
	
	$(".sys-menu").mouseover(function(){
		$("#navigate").hide();
	});
	
	$(".index-logo").mouseover(function(){
		$("#navigate").hide();
	});

	$(".index-body").mouseover(function(){
		$("#navigate").hide();
	});
	
	$(".logout-btns").mouseover(function(){
		$(".logout-btns").addClass("logout-btns-select");
	});
	
	$(".logout-btns").mouseout(function(){
		$(".logout-btns").removeClass("logout-btns-select");
	});
	
	$(".person-set-btns").mouseover(function(){
		$(".person-set-btns").addClass("person-set-btns-select");
	});
	
	$(".person-set-btns").mouseout(function(){
		$(".person-set-btns").removeClass("person-set-btns-select");
	});
	
	
	$(".user-info").mouseout(function(){
		$("#navigate").mouseover(function(){
			$("#navigate").show();
		});
		
		$("#navigate").mouseout(function(){
			$("#navigate").hide();
		});
	});
	$(".room-map-close").click(function(){
		$(".index-dialog-cover").hide();
		$(".room-map-dialog").hide();
	});
}


function changePage(pageurl){
	$("#main-body").attr("src",pageurl);
}